package com.pattern.beforeclass.proxy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author Ping
 * @Date 2022/4/11 11:23
 **/
public class FlyweightDemo {
    static String From = "FUZHOU" ;
    static List<String> Tos = List.of("XIAMEN", "QUANZHOU","HANGZHOU") ;
    static List<String> Intervals = List.of("8AM", "10AM", "2PM","10PM") ;

    public static void main(String args[]) {
        String str1 = "abcd";
        String str2 = "abcd";
        String str3 = "ab" + "cd";
        String str4 = "ab";
        str4 += "cd";
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
        System.out.println(str1 == str4);



    }

}

class InnerTicket {
    String id ;
    String from ;
    String target ;
    String timeInterval ;

    public InnerTicket(String from, String target, String timeInterval) {
        id = from + "_" + target +"_" + "_" + timeInterval ;
        this.from = from;
        this.target = target;
        this.timeInterval = timeInterval;
    }
    public String getKey() {
        return this.id ;
    }
    @Override
    public String toString() {
        return "{" +
                "from='" + from + '\'' +
                ", target='" + target + '\'' +
                ", timeInterval='" + timeInterval + '\'' +
                '}';
    }
}

class TicketFactory {
    static Map<String, InnerTicket> ticketPool = new ConcurrentHashMap<>();
    public static InnerTicket getTicket(String key) {
        return ticketPool.get(key) ;
    }
    public static void addToTicketPool(InnerTicket ticket) {
        ticketPool.put(ticket.getKey(), ticket) ;
    }
}

class TranTicket {
    static final String SOLD_OUT = "out" ;
    static final String IN_SOLD = "in" ;
    InnerTicket ticketInfo ;
    String owner ;
    String status ;

    public TranTicket(String key) {
        this.ticketInfo = TicketFactory.getTicket(key);
        this.status = IN_SOLD;
    }
    public void sell(String owner) {
        this.owner = owner ;
        status = SOLD_OUT ;
    }
    public boolean isSoldOut() {
        return status == SOLD_OUT ;
    }

    @Override
    public String toString() {
        return "TranTicket{" +
                "ticketInfo=" + ticketInfo +
                ", owner='" + owner + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}


